package com.atguigu.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.product.entity.AttrEntity;
import com.atguigu.product.entity.AttrGroupEntity;
import com.atguigu.product.service.AttrAttrgroupRelationService;
import com.atguigu.product.service.AttrService;
import com.atguigu.product.service.CategoryService;
import com.atguigu.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.product.vo.AttrRelationVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author litong
 * @email 2943284183@qq.com
 * @date 2024-08-07 18:11:07
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

//    发布商品中的规格参数
    @GetMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    @PostMapping("/attr/relation")
    public R relationSave(@RequestBody List<AttrRelationVo> attrRelationVoList){
        relationService.relationSave(attrRelationVoList);
        return R.ok();
    }

//    删除关联属性(包括批量删除)
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrRelationVo[] attrRelationVos){
        System.out.println("====");
        attrGroupService.removeRelation(attrRelationVos);

        return R.ok();
    }

//    查询分组下面的所有基本属性(即已经关联的)
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntityList = attrService.groupIdGetAttr(attrgroupId);
        return R.ok().put("data",attrEntityList);

    }

    //    查询当前分组(attrgroupId)下面的还没有的基本属性(即没有关联的)
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.groupIdGetNoAttr(attrgroupId,params);
        return R.ok().put("page",page);

    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.PageCatIdAndKey(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        System.out.println("--->"+attrGroupId.toString());
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] longs=null;
        if(attrGroup!=null){
            System.out.println("不是空");
            longs= categoryService.findAllPath(attrGroup.getCatelogId());
        }
        attrGroup.setCatelogPath(longs);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
